package com.feicheng.iou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.iou.entity.Permission;
import com.feicheng.iou.entity.RolePermission;
import com.feicheng.iou.mapper.PermissionMapper;
import com.feicheng.iou.pojo.UpdateRolePermissionPOJO;
import com.feicheng.iou.pojo.login.PermissionLevelTwoPOJO;
import com.feicheng.iou.pojo.login.PermissionListPOJO;
import com.feicheng.iou.pojo.login.PermissionSelfJoinPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@Service
@Slf4j
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * @Author wuweinan
     * @Description 格式化并且封装用户的权限列表
     * @Date 2021-7-23 10:45
     * @param permissionIds
     * @return  List<PermissionListPOJO>
     */
    @Override
    public List<PermissionListPOJO> selectPermissionsList(List<Integer> permissionIds,Boolean showAllFlg) {
        log.info("PermissionServiceImpl的selectPermissionsList方法进入，" +
                "参数permissionIds：" + permissionIds+",参数showAllFlg： " +showAllFlg);

        //获取用户的permissionList
        List<PermissionSelfJoinPOJO> PermissionSelfJoinPOJOs = permissionMapper.selectSelfJoinPermission(permissionIds);

        List<PermissionListPOJO> permissions = new ArrayList<>();

        //获取用户的一级权限                                   //
        for (PermissionSelfJoinPOJO permissionSelfJoinPOJO : PermissionSelfJoinPOJOs) {
            if (permissionSelfJoinPOJO.getFatherPermissionId() == null) {
                PermissionListPOJO permissionListPOJO = new PermissionListPOJO();
                permissionListPOJO.setPermissionId(permissionSelfJoinPOJO.getPermissionId());
                permissionListPOJO.setName(permissionSelfJoinPOJO.getPermissionName());
                permissions.add(permissionListPOJO);
            }
        }

        //将权限与父权限的自关联列表列表封装到对象中
        for (PermissionListPOJO permissionListPOJO : permissions) {
            List<PermissionLevelTwoPOJO> permissionLevelTwoPOJOS = new ArrayList<>();
                                                                  //
            for (PermissionSelfJoinPOJO permissionSelfJoinPOJO : PermissionSelfJoinPOJOs) {

                if (permissionSelfJoinPOJO.getFatherPermissionId() != null) {  //二级菜单

                    if (permissionSelfJoinPOJO.getFatherPermissionId() == permissionListPOJO.getPermissionId()) {

                        PermissionLevelTwoPOJO permissionLevelTwoPOJO = new PermissionLevelTwoPOJO();
                        permissionLevelTwoPOJO.setPermissionId(permissionSelfJoinPOJO.getPermissionId());
                        permissionLevelTwoPOJO.setName(permissionSelfJoinPOJO.getPermissionName());
                        permissionLevelTwoPOJO.setPath(permissionSelfJoinPOJO.getPath());
                        permissionLevelTwoPOJO.setType(permissionSelfJoinPOJO.getType());
                        permissionLevelTwoPOJOS.add(permissionLevelTwoPOJO);
                    }
                }
                permissionListPOJO.setPermissions(permissionLevelTwoPOJOS);
            }
        }
        return permissions;
    }


    /**
     * @Author wuweinan
     * @Description 按权限分级嵌套查询所有权限
     * @Date 2021-7-23 10:45
     * @param null
     * @return  ResultInfo
     */
    @Override
    public ResultInfo selectAllPermission() {
        log.info("PermissionServiceImpl的selectAllPermission方法进入");
        //查询出所有权限
        List<Permission> permissions = permissionMapper.selectList(null);
        //封装所有的权限ID
        List<Integer> allPermissionIds = new ArrayList<>();

        for (Permission permission : permissions) {
            allPermissionIds.add(permission.getPId());
        }

        //调用上面的selectPermissionsList方法,将所有权限按级别嵌套查询
        List<PermissionListPOJO> permissionListPOJOS = selectPermissionsList(allPermissionIds,true);

        return ResultInfo.success(permissionListPOJOS);
    }

    /**
     * @Author wuweinan
     * @Description 查询所有的权限
     * @Date 2021-7-23 10:45
     * @param null
     * @return  ResultInfo
     */
    @Override
    public ResultInfo selectPermissionList() {
        log.info("PermissionServiceImpl的selectPermissionList方法进入");
        List<Permission> permissions = permissionMapper.selectList(null);
        return ResultInfo.success(permissions);
    }
}
